﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autocentro.Entities;
using Autocentro.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Autocentro.UI
{
    public partial class FrmMttoTipoVehiculo : Form
    {
        [Dependency]
        public IEmpresaBL empresabl { get; set; }

        [Dependency]
        public ITipoVehiculoBL tipovehiculobl { get; set; }

        public FrmMttoTipoVehiculo()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public TIPO_VEHICULO CurrentTipoVehiculo { get; set; }
        bool isnew;

        public void Nuevo()
        {
            tIPO_VEHICULOBindingSource.AddNew();
            isnew = true;
        }

        public void Editar()
        {
            tIPO_VEHICULOBindingSource.DataSource = CurrentTipoVehiculo;
            isnew = false;
        }


        private void CBEmpresa_SelectedValueChanged(object sender, EventArgs e)
        {
            if (CBEmpresa.SelectedValue != null)
            {
                empresaIdTextBox.Text = CBEmpresa.SelectedValue.ToString();
            }
          
        }

        private void CBEmpresa_Click(object sender, EventArgs e)
        {
            eMPRESABindingSource.DataSource = empresabl.GetAllFromEmpresa();
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var _tipovehiculo = tIPO_VEHICULOBindingSource.Current as TIPO_VEHICULO;
            var msg = "ERROR";

            if (_tipovehiculo != null)
            {
                _tipovehiculo.Descripcion = descripcionTextBox.Text.ToUpper();

                if (isnew)
                {
                    try
                    {
                        tipovehiculobl.AddTipoVehiculo(_tipovehiculo);
                        MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    try
                    {
                        tipovehiculobl.UpdateTipoVehiculo(_tipovehiculo);
                        MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
   
            }
        }
    }
}
